package com.fz.song.service;


import com.fz.song.enums.ExceptionEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.mapping.SysResourceMapper;
import com.fz.song.mapping.SysRoleResourceMapper;
import com.fz.song.pojo.SysResource;
import com.fz.song.pojo.SysRoleResource;
import com.fz.song.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    public SysResourceMapper sysResourceMapper;
    @Autowired
    public SysRoleResourceMapper sysRoleResourceMapper;


    public List<SysResource> queryResource() {
        //第一步查询父菜单
        List<SysResource> tops = sysResourceMapper.getTopResource();
        //遍历父菜单,查询子菜单
        for (SysResource top : tops) {
            Integer sysid = top.getSysid();
            List<SysResource> childs = sysResourceMapper.getTopResourceByPid(sysid);
            top.setChilds(childs);
        }
        return tops;
    }

    public List<SysResource> queryResourceAll() {
        return sysResourceMapper.getTopResourceAll();
    }


    public SysResource getResourceById(int sysid) {
        SysResource sysResource = sysResourceMapper.selectByPrimaryKey(sysid);
        return sysResource;
    }

    public int saveResource(SysResource sysResource) {
        Integer sysid = sysResource.getSysid();
        if (sysid == 0)
            return sysResourceMapper.insertSelective(sysResource);
        return sysResourceMapper.updateByPrimaryKeySelective(sysResource);
    }

    public List<SysRoleResource> queryHaveResource(int sysid) {
        return sysRoleResourceMapper.queryHaveResource(sysid);
    }

    public List<SysRoleResource> queryRoleResource(Integer roleid) {
        return sysRoleResourceMapper.queryHaveResource(roleid);
    }
}
